package com.wskj.web.controller.system;

import java.util.List;
import java.util.Arrays;

import lombok.AllArgsConstructor;

import javax.validation.constraints.*;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;
import com.wskj.common.annotation.RepeatSubmit;
import com.wskj.common.annotation.Log;
import com.wskj.common.core.controller.BaseController;
import com.wskj.common.core.domain.AjaxResult;
import com.wskj.common.enums.BusinessType;
import com.wskj.common.exception.CustomException;
import com.wskj.system.vo.web.SysFeedbackVo;
import com.wskj.system.bo.SysFeedbackQueryBo;
import com.wskj.system.bo.SysFeedbackAddBo;
import com.wskj.system.bo.SysFeedbackEditBo;
import com.wskj.system.service.ISysFeedbackService;
import com.wskj.common.utils.poi.ExcelUtil;
import com.wskj.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

/**
 * 意见反馈Controller
 *
 * @author wsdz
 * @date 2022-08-22
 */
@Api(value = "意见反馈控制器", tags = {"意见反馈管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/workbench/feedback")
public class SysFeedbackController extends BaseController {

    private final ISysFeedbackService iSysFeedbackService;

    /**
     * 查询意见反馈列表
     */
    @ApiOperation("查询意见反馈列表")
    @PreAuthorize("@ss.hasPermi('workbench:feedback:list')")
    @GetMapping("/list")
    public TableDataInfo<SysFeedbackVo> list(@Validated SysFeedbackQueryBo bo) {
        return iSysFeedbackService.queryPageList(bo);
    }

    /**
     * 导出意见反馈列表
     */
    @ApiOperation("导出意见反馈列表")
    @PreAuthorize("@ss.hasPermi('workbench:feedback:export')")
    @Log(title = "意见反馈", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<Void> export(@Validated SysFeedbackQueryBo bo) {
        List<SysFeedbackVo> list = iSysFeedbackService.queryList(bo);
        ExcelUtil<SysFeedbackVo> util = new ExcelUtil<>(SysFeedbackVo.class);
        return util.exportExcel(list, "意见反馈");
    }

    /**
     * 获取意见反馈详细信息
     */
    @ApiOperation("获取意见反馈详细信息")
    @PreAuthorize("@ss.hasPermi('workbench:feedback:query')")
    @GetMapping("/{feedbackId}")
    public AjaxResult<SysFeedbackVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("feedbackId") Long feedbackId) {
        return AjaxResult.success(iSysFeedbackService.queryById(feedbackId));
    }

    /**
     * 新增意见反馈
     */
    @ApiOperation("新增意见反馈")
    @PreAuthorize("@ss.hasPermi('workbench:feedback:add')")
    @Log(title = "意见反馈", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody SysFeedbackAddBo bo) {
        return toAjax(iSysFeedbackService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改意见反馈
     */
    @ApiOperation("修改意见反馈")
    @PreAuthorize("@ss.hasPermi('workbench:feedback:edit')")
    @Log(title = "意见反馈", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody SysFeedbackEditBo bo) {
        return toAjax(iSysFeedbackService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除意见反馈
     */
    @ApiOperation("删除意见反馈")
    @PreAuthorize("@ss.hasPermi('workbench:feedback:remove')")
    @Log(title = "意见反馈" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{feedbackIds}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] feedbackIds) {
        return toAjax(iSysFeedbackService.deleteWithValidByIds(Arrays.asList(feedbackIds), true) ? 1 : 0);
    }

    /**
     * 导入意见反馈数据
     */
    @ApiOperation("导入意见反馈数据")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "file")
    @PreAuthorize("@ss.hasPermi('workbench:feedback:import')")
    @Log(title = "意见反馈" , businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult<Void> importData(MultipartFile file) {

        ExcelUtil<SysFeedbackVo> util = new ExcelUtil<>(SysFeedbackVo.class);
        List<SysFeedbackVo> sysFeedbackList;
        try {
            sysFeedbackList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("文件解析异常！message：" + e.getMessage());
        }
        String message = iSysFeedbackService.importSysFeedback(sysFeedbackList);
        return AjaxResult.success(message);

    }

    /**
     * 下载模板
     *
     * @return AjaxResult 模板文件
     */
    @ApiOperation("下载模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<SysFeedbackVo> util = new ExcelUtil<>(SysFeedbackVo.class);
        return util.importTemplateExcel("意见反馈数据");
    }
}
